﻿using System;
using System.Collections.Generic;
using System.Text;
using sb0t_winGUI;

namespace cb0tServer
{
    partial class ServerCore
    {
        private List<QuizItem> QuizQuestions = new List<QuizItem>();
        private List<QuizUser> QuizUsers = new List<QuizUser>();
        private bool QuizOn = false;
        private int QuizState = 0;
        private int QuizTick = 0;
        private int QuizPosition = 0;
        private int QuizAnswerCount = 0;
        private Random QuizRandom = new Random();

        private void QuizStart(UserObject userobj)
        {
            QuizQuestions = new List<QuizItem>(Quiz.CurrentList);

            if (QuizQuestions.Count == 0)
            {
                UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(132)));
                return;
            }

            QuizUsers.Clear();
            QuizOn = true;
            QuizState = 0;
            QuizTick = 0;
            QuizPosition = 0;
            QuizAnswerCount = 0;
            UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(133).Replace("+n", userobj.name)));
            UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(""));
            PrintQuestion();
        }

        private void QuizStop(UserObject userobj)
        {
            UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(134).Replace("+n", userobj.name)));
            QuizOn = false;
            QuizQuestions.Clear();
            QuizUsers.Clear();
        }

        private void PrintHint(UserObject userobj)
        {
            String ph_answer = QuizQuestions[QuizPosition].Answer;
            int ph_index = (int)Math.Floor(QuizRandom.NextDouble() * ph_answer.Length);

            while (ph_answer[ph_index] == ' ')
                ph_index = (int)Math.Floor(QuizRandom.NextDouble() * ph_answer.Length);

            String ph_result = "";

            for (int i = 0; i < ph_answer.Length; i++)
            {
                if (i == ph_index)
                    ph_result += ph_answer[i];
                else
                    ph_result += (ph_answer[i] == ' ' ? " " : "*");
            }

            userobj.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(135).Replace("+h", ph_result)));
        }

        private void PrintQuestion()
        {
            UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(136).Replace("+x", (QuizPosition + 1).ToString()).Replace("+y", QuizQuestions.Count.ToString())));
            UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(""));
            UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(137).Replace("+q", QuizQuestions[QuizPosition].Question)));
            UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(""));
        }

        private void PrintAnswerAndResults()
        {
            UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(138)));
            UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(""));
            UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(139).Replace("+a", QuizQuestions[QuizPosition].Answer)));
            UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(""));
            UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(140)));
            UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(""));

            if (QuizAnswerCount == 0)
            {
                UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(141)));
                UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(""));
                return;
            }
            else
            {
                for (int i = 0; i < QuizUsers.Count; i++)
                {
                    if (QuizUsers[i].Answered > 0)
                    {
                        UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(142).Replace("+n", QuizUsers[i].Name).Replace("+p", QuizUsers[i].Answered.ToString())));
                        QuizUsers[i].Points += QuizUsers[i].Answered;
                        QuizUsers[i].Answered = 0;
                    }
                }
            }

            UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(""));
        }

        private bool NextQuestion()
        {
            QuizPosition++;
            QuizAnswerCount = 0;
            return QuizPosition < QuizQuestions.Count;
        }

        private void CheckAnswer(UserObject userobj, String text)
        {
            String ca_required = QuizQuestions[QuizPosition].Answer.ToUpper();
            String ca_answer = text.Trim().ToUpper();

            if (ca_answer.Contains(ca_required))
            {
                int ui = QuizFindUserIndex(userobj);

                if (QuizUsers[ui].Answered > 0)
                    return;

                if (QuizAnswerCount == 0)
                {
                    userobj.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(143).Replace("+n", userobj.name)));
                    QuizUsers[ui].Answered = 3;
                }

                if (QuizAnswerCount == 1)
                {
                    userobj.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(144).Replace("+n", userobj.name)));
                    QuizUsers[ui].Answered = 2;
                }

                if (QuizAnswerCount > 1)
                {
                    userobj.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(145).Replace("+n", userobj.name)));
                    QuizUsers[ui].Answered = 1;
                }

                QuizAnswerCount++;
            }
        }

        private int QuizFindUserIndex(UserObject userobj)
        {
            int i = QuizUsers.FindIndex(x => x.Name == userobj.name && x.Guid.Equals(userobj.guid));

            if (i > -1)
                return i;

            QuizUsers.Add(new QuizUser(userobj));
            return QuizUsers.Count - 1;
        }

        private void PrintResultsAndQuit()
        {
            QuizOn = false;
            QuizQuestions.Clear();
            UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(146)));
            UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(""));

            if (QuizUsers.Count == 0)
            {
                UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(147)));
                UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(""));
                QuizQuestions.Clear();
                return;
            }

            int praq_highest = 0;

            for (int i = 0; i < QuizUsers.Count; i++)
            {
                UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(148).Replace("+n", QuizUsers[i].Name).Replace("+p", QuizUsers[i].Points.ToString())));

                if (QuizUsers[i].Points > praq_highest)
                    praq_highest = QuizUsers[i].Points;
            }

            UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(""));
            String praq_winner = GetWinner(praq_highest);

            for (int i = 0; i < 6; i++)
                UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(praq_winner));
        }

        private String GetWinner(int high_score)
        {
            List<QuizUser> gw_list = QuizUsers.FindAll(x => x.Points == high_score);

            if (gw_list.Count == 1)
                return StringTemplate.GetString(149).Replace("+n", gw_list[0].Name);

            String gw_result = "";

            for (int i = 0; i < gw_list.Count; i++)
                if (i == (gw_list.Count - 1))
                    gw_result += gw_list[i].Name;
                else
                    gw_result += gw_list[i].Name + ", ";

            return StringTemplate.GetString(150).Replace("+n", gw_result);
        }

        private void QuizOnTimer()
        {
            if (QuizOn)
            {
                if (QuizState == 0)
                {
                    QuizTick++;

                    if (QuizTick == 15)
                    {
                        QuizTick = 0;
                        QuizState = 1;
                        PrintAnswerAndResults();
                    }
                }
                else
                {
                    QuizTick++;

                    if (QuizTick == 8)
                    {
                        QuizTick = 0;
                        QuizState = 0;
                        
                        if (NextQuestion())
                            PrintQuestion();
                        else
                            PrintResultsAndQuit();
                    }
                }
            }
        }

        private void QuizOnText(UserObject userobj, String text)
        {
            if (QuizOn && QuizState == 0)
            {
                if (userobj.vroom == 0)
                {
                    CheckAnswer(userobj, text);

                    if (text == "hint")
                        PrintHint(userobj);
                }
            }
        }
    }

    class QuizUser
    {
        public Guid Guid;
        public String Name;
        public int Answered;
        public int Points;

        public QuizUser(UserObject target)
        {
            this.Guid = target.guid;
            this.Name = target.name;
            this.Answered = 0;
            this.Points = 0;
        }
    }
}
